<?php
/**
 * XIE--Scorpion
 *
 * 用户操作
 */
namespace App\Models\Home;

use DB,Session,Redirect;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
	protected $table = 'user';
    public $timestamps = false;

	/**
	 *用户登录
	 */
	static function login($user_email,$user_pwd)
	{ 
	   //检测用户名和密码输入是否正确
       $re=DB::table('user')->where('user_email',$user_email)->where('user_pwd',$user_pwd)->get();
	   if($re)
	   {    //取出用户名和对应的ID
            $arr=DB::select("select user_name,user_id from user where user_email='$user_email'");
		    foreach($arr as $v)
		    {
		       $user_name=$v->user_name;
		       $user_id=$v->user_id;
		    }
			//将用户名和对应的ID存入Session
              Session::put('user_name',$user_name);
			  Session::put('user_id',$user_id);

		}else
		{
            echo "<script>alert('登陆失败');</script>";
			return view('homes/login/login');
		}
	}

	/**
	 *用户注册
	 */
	static function register($data)
	{
		//注册信息入库并且取出相应的ID
		$user_id=DB::table('user')->insertGetid($data);	
        if($user_id)
		{
		   //将用户名和对应的ID存入Session
		   Session::put('user_name',$data['user_name']);
		   Session::put('user_id',$user_id);
           echo "<script>alert('注册成功');</script>";
            return view('homes/login/login');
	    }else
		{
           echo "<script>alert('注册失败');</script>";
		   return view('homes/login/register');
		}       
	}

} 